package brainbots.gui;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;
import javax.swing.SwingUtilities;


public class BotsVp extends JComponent {

    private BotsView view = new BotsView();
    private BotControl controller = new BotControl();
    
    
    public BotsVp() {
        setFocusable(true);
        
        addKeyListener(controller);
        addMouseListener(controller);
        
        controller.setView(view);
    }
    
    public void setView(BotsView view) {
        this.view = view;
        controller.setView(view);
        
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        g.clearRect(0, 0, getWidth(), getHeight());
        
        if (view != null){
            view.draw((Graphics2D)g);
        }
    }
    
    /**Returns updater instance, that can be called to request component repaint*/
    public Runnable newUpdater(){
        return new Runnable(){
            public void run(){
                SwingUtilities.invokeLater(new Runnable(){
                    public void run(){
                        repaint();
                    }
                });
            }
        };
    }
    
//    public static void main(String[] args) {
//        JFrame frm = new JFrame("Test frame");
//        frm.setSize(new Dimension(800,600));
//        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
//        
//        
//        ////////////////////////
//        //Create world
//        ///////////////////////////////
//        final World world = new World();
//        
//        //add user-controlled bot
//        ManuallyControlledBrain mBrain = new ManuallyControlledBrain();
//        world.addBot(200, 200, 0.9, mBrain);
//        
//        
//        //add random bots
//        for(int i=0;i<100;++i){
//        	BrainBot bot = new BrainBot(400,300,0.6);
//        	MatrixBrain brain = new MatrixBrain(bot,4,4);
//        	brain.randomInit();
//        	bot.setBrain(brain);
//        	world.addBot(bot);
//            //Brain randomBrain = new RandomBrain();
//            //world.addBot(400,300,0.6, randomBrain);
//        }
//        
//        //populate with food
//        for (int i = 0; i<300; ++i){
//            world.addFood();
//        }
//        //add breeder
//        MatrixBrainBreeder breeder = new MatrixBrainBreeder();
//        world.setBreeder(breeder);
//        
//        //Create wiewport
//        final BotsVp simVp = new BotsVp();
//        simVp.view.setWorld(world);
//        
//        mBrain.attach(simVp);
//        
//        frm.getContentPane().add(simVp); //and put viewport to the frame
//                
//
//        InteractiveSimulator sim = new InteractiveSimulator();
//        sim.setWorld(world);
//        sim.setDelay(0);
//        sim.setDt(0.1);
//        
//        //creating the simulator
//        /*
//        SynchronousSimulator sim = new SynchronousSimulator(world);
//        sim.setStepHandler(simVp.newUpdater()); //when step is performed, updater called
//        sim.setDelay(100);
//        sim.setTimeScale(50.0);
//        sim.setTimeGrain(0.05);
//        */
//        
//        world.setCellUpdateInterval(.5);
//        
//        //create thread with this simulator
//        final Thread simThread = new Thread(sim);
//        simThread.setDaemon(true);
//
//        //create redraw thread
//        final Runnable runUpdateVp=new Runnable(){
//			@Override
//			public void run() {
//				simVp.repaint();
//			}
//        };
//        final Runnable updater = new Runnable(){
//			@Override
//			public void run() {
//				while (true){
//					try {
//						SwingUtilities.invokeAndWait(runUpdateVp);
//						Thread.sleep(100);
//					} catch (InterruptedException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					} catch (InvocationTargetException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//					
//				}
//			}
//        };
//        Thread updThread = new Thread(updater);
//        updThread.setDaemon(true);
//        
//        //show windows
//        frm.setVisible(true);
//        
//        //and run the simulation
//        simThread.start();
//        updThread.start();
//        
//        System.out.println("Started simulation");
//    }
}
